package cn.edu.scau.cmi.dengzejun.chapter11;

import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.io.*;
import java.awt.event.*;
import javax.swing.event.*;

public class LP extends JLayeredPane implements MouseListener,MouseMotionListener{
	
	JButton button;
	int x,y,a,b,x0,y0;
	LP(){
		this.button = new JButton("Please drag me~");
		this.button.addMouseListener(this);
		this.button.addMouseMotionListener(this);
		this.setLayout(new FlowLayout());
		this.add(this.button, JLayeredPane.DEFAULT_LAYER);
		return;
	}
	
	@Override
	public void mouseClicked(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub
		JComponent com = null;
		com = (JComponent)arg0.getSource();
		this.setLayer(com, JLayeredPane.DRAG_LAYER);
		this.a  = com.getBounds().x;
		this.b = com.getBounds().y;
		this.x0 = arg0.getX();
		this.y0 = arg0.getY();
		return;
	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub
		JComponent com = null;
		com = (JComponent)arg0.getSource();
		this.setLayer(com, JLayeredPane.DEFAULT_LAYER);
		return;
	}

	@Override
	public void mouseDragged(MouseEvent arg0) {
		// TODO Auto-generated method stub
		Component com = null;
		if (arg0.getSource() instanceof Component) {
			com = (Component)arg0.getSource();
			this.a = com.getBounds().x;
			this.x = arg0.getX();
			this.y = arg0.getY();
			this.a +=x;
			this.b +=y;
			com.setLocation(this.a-this.x0,this.b-this.y0);
		}
		return;
	}

	@Override
	public void mouseMoved(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	
}
